<template>
	<view>
		<view class='top-wrap'>
			<view class='mark-wrap clearfix'>
				<view class='title'>我的战绩</view>
				<view class="mark-btn">
					<image class='icon' src='../../../static/images/earlycolock/plane.png'></image>
					<button class='mark-btn' open-type="share">晒战绩</button>
				</view>
			</view>
			<view class='money-layout'>
				<view class='money-item'>
					<text class='money'>{{earlyUserInfo.totalInput/100||0}}</text>
					<text class='money-msg'>累计投入(元)</text>
				</view>
				<view class='money-item'>
					<text class='money'>{{earlyUserInfo.totalIncome/100||0}}</text>
					<text class='money-msg'>累计赚取(元)</text>
				</view>
				<view class='money-item'>
					<text class='money'>{{earlyUserInfo.successDays||0}}</text>
					<text class='money-msg'>成功打卡(天)</text>
				</view>
			</view>
		</view>
		<view class='sign-list'>
			<image mode='aspectFit' src='../../../static/images/earlycolock/signIcon.png' class='signIcon'></image>
			<text class='sign-msg'>打卡明细</text>
		</view>
		<uni-table width="100%" stripe emptyText="暂无更多数据">
			<!-- 表头行 -->
				<uni-tr>
					<uni-th align="center">报名时间</uni-th>
					<uni-th align="center">打卡状态</uni-th>
					<uni-th align="center">打卡时间</uni-th>
					<uni-th align="center">打卡投入</uni-th>
					<uni-th align="center">打卡奖励</uni-th>
				</uni-tr>
				<uni-tr v-for="(item,index) in recordList" :key="index">
					<uni-th align="center">{{item.createTime}}</uni-th>
					<uni-th align="center">{{item.colockStatus==1?'已打卡':'未打卡'}}</uni-th>
					<uni-th align="center">{{item.colcokTime||'暂未打卡'}}</uni-th>
					<uni-th align="center">+{{item.colockInput/100||0}}</uni-th>
					<uni-th align="center"><text style="color: red;">+{{item.colockIncome/100||0}}</text></uni-th>
				</uni-tr>
		</uni-table>
		
		<view class="ad-container">
			<view class="ad">
				<ad unit-id="adunit-619582f818cb0d1d" ad-intervals="30"></ad>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
		mapMutations
	} from 'vuex'
	import {
		http
	} from '../../../utils/http.js'
	export default {
		data() {
			return {
				earlyUserInfo: {},
				recordList: [],
				currentPage: 1,
				size: 10,
				total: 0,
				isTotal: true,
				share: {
					title: '早起打卡，养成好习惯，打卡瓜分奖池，赢🧧',//自定义标题
					path: `/subpkg/earlyclock/index/index`,  //默认跳转首页
					imageUrl:'https://cos.xunmeng.icu/cartoonrabbit/static/earlyshare.png'
				}
			};
		},
		computed: {
			...mapState('m_user', ['userInfo', 'token']),
		},
		methods: {
			...mapMutations('m_user', ['updateUserInfo', 'updateToken']),
			async flashRecordList() {
				let that = this
				const res = await http({
					url: '/early/user/info',
					method: 'GET',
					header: {
						Authorization: that.token.data
					}
				})
				// console.log(res)
				if (res.code == 200) {
					that.earlyUserInfo = res.data
				} else {
					uni.showToast({
						title: res.message,
						duration: 2000,
						icon: "none"
					})
				}
			},
			async flashEarlyUserInfo() {
				let that = this
				let res = await http({
					url: '/early/user/recordList' + "?currentPage=" + that.currentPage + "&size=" + that.size,
					method: "GET",
					header: {
						Authorization: that.token.data
					}
				})
				// console.log(res)
				if (res.code && res.code == 200) {
					if (that.recordList && that.recordList.length > 0) {
						that.recordList = that.recordList.concat(res.data.data)
					} else {
						that.recordList = res.data.data
					}
					if (res.data.total > res.data.currentPage * res.data.size) {
						that.isTotal = false
					} else {
						that.isTotal = true
					}
					that.currentPage = res.data.currentPage
					that.size = res.data.size
					that.total = res.data.total
				} else {
					uni.showToast({
						duration: 2000,
						title: '请求数据失败',
						icon: "none"
					})
				}
			},
			// 滚动条触底刷新
			onReachScollBottom() {
				if (!this.isTotal) {
					this.currentPage = this.currentPage + 1
					this.flashRecordList()
				} else {
					uni.showToast({
						duration: 2000,
						title: '已经是全部数据啦',
						icon: "none"
					})
				}
			}
		},
		onLoad() {
			this.flashEarlyUserInfo();
			this.flashRecordList()
		},
		onReachBottom() {
			this.onReachScollBottom()
		},
		async onShareAppMessage(res) { //发送给朋友
			let that = this
			// 动态获取当前页面栈
			let pages = getCurrentPages(); //获取所有页面栈实例列表
			let nowPage = pages[pages.length - 1]; //当前页页面实例
			// let prevPage = pages[pages.length - 2]; //上一页页面实例
			// that.share.path = `/${nowPage.route}`
			// 获取token
			let token=uni.getStorageSync('token')
			if(token){
				token=JSON.parse(token)
			}else{
				await uni.showToast({
					title: '未授权请先登录',
					icon: 'none'
				})
				return
			}
			// 获取用户的唯一标识
			const resoult=await http({
				url:'/user/useronlyId',
				method:"GET",
				header:{
					Authorization:token.data
				}
			})
			let params=''
			if(resoult.code==200){
				params='?scene='+resoult.data
			}else{
				await uni.showToast({
					title: '获取参数异常',
					icon: 'none'
				})
				return
			}
			return {
				title: this.share.title,
				path: this.share.path+params,
				imageUrl: this.share.imageUrl,
				funtion:(function() {
					uni.showToast({
						title: '分享成功'
					})
					// 判断制作次数是否可以加一
					// 获取是否可分享的时间戳与当前时间戳进行对比
					let canGetProductionTime= uni.getStorageSync('canGetProductionTime')
					// 与当前时间进行对比
					const currentTimestamp = Date.parse(new Date());
					if(!canGetProductionTime||((currentTimestamp-canGetProductionTime)/60/60/1000>24)){
						// 调用制作次数加一
						if(token.data){
							
							http({
								url:'/user/production/change/1',
								method:"POST",
								header:{
									Authorization:token.data
								}
							}).then(res=>{
								if(res.code==200){
									uni.showToast({
										title: '制作次数+1',
										icon: 'none'
									})
									uni.setStorageSync('canGetProductionTime',currentTimestamp)
									that.$store.commit('m_user/updateUserInfo',res.data)
								}
							})
						}
					}else{
						uni.setStorageSync('canGetProductionTime',currentTimestamp)
					}
					
				})()
			}
		},
		async onShareTimeline(res) { //分享到朋友圈
		let that = this
			// 动态获取当前页面栈
			let pages = getCurrentPages(); //获取所有页面栈实例列表
			let nowPage = pages[pages.length - 1]; //当前页页面实例
			// let prevPage = pages[pages.length - 2]; //上一页页面实例
			// that.share.path = `/${nowPage.route}`
			// 获取token
			let token=uni.getStorageSync('token')
			if(token){
				token=JSON.parse(token)
			}else{
				await uni.showToast({
					title: '未授权请先登录',
					icon: 'none'
				})
				return
			}
			// 获取用户的唯一标识
			const resoult=await http({
				url:'/user/useronlyId',
				method:"GET",
				header:{
					Authorization:token.data
				}
			})
			let params=''
			if(resoult.code==200){
				params='?scene='+resoult.data
			}else{
				await uni.showToast({
					title: '获取参数异常',
					icon: 'none'
				})
				return
			}
			return {
				title: this.share.title,
				path: this.share.path+params,
				imageUrl: this.share.imageUrl,
				funtion:(function() {
					uni.showToast({
						title: '分享成功'
					})
					// 判断制作次数是否可以加一
					// 获取是否可分享的时间戳与当前时间戳进行对比
					let canGetProductionTime= uni.getStorageSync('canGetProductionTime')
					// 与当前时间进行对比
					const currentTimestamp = Date.parse(new Date());
					if(!canGetProductionTime||((currentTimestamp-canGetProductionTime)/60/60/1000>24)){
						// 调用制作次数加一
						// 获取token
						let token=uni.getStorageSync('token')
						if(token){
							token=JSON.parse(token)
						}
						if(token.data){
							
							http({
								url:'/user/production/change/1',
								method:"POST",
								header:{
									Authorization:token.data
								}
							}).then(res=>{
								if(res.code==200){
									uni.showToast({
										title: '制作次数+1',
										icon: 'none'
									})
									uni.setStorageSync('canGetProductionTime',currentTimestamp)
									that.$store.commit('m_user/updateUserInfo',res.data)
								}
							})
						}
					}else{
						uni.setStorageSync('canGetProductionTime',currentTimestamp)
					}
					
				})()
			}
		},
	}
</script>

<style>
	page {
		background: #FFFFFF;
	}
</style>
<style lang="scss" scoped>
	/* pages/my-mark/my-mark.wxss */
	.top-wrap {
		box-sizing: border-box;
		height: 390rpx;
		background-image: url();
		background-size: 100% 100%;
		padding: 80rpx 58rpx;
	}

	.mark-wrap {
		/* overflow: hidden; */
		margin-bottom: 80rpx;
	}

	.mark-btn {
		width: 200rpx;
		height: 48rpx;
		color: #ffffff;
		/* margin-top: 50rpx; */
		background: #0c4a61;
		border-top-left-radius: 20rpx;
		border-bottom-left-radius: 20rpx;
		display: flex;
		font-size: 26rpx;
		position: relative;
		/* padding-left: 74rpx; */
		align-items: center;
		float: right;
		box-sizing: border-box;
		margin-right: -58rpx;
	}

	.icon {
		width: 50rpx;
		height: 56rpx;
		position: absolute;
		left: 10rpx;
		bottom: 0;

	}

	.title {
		max-width: 480rpx;
		color: #ffffff;
		/* padding-top: 80rpx; */
		overflow: hidden;
		text-overflow: ellipsis;
		float: left;
		font-size: 55rpx;
	}

	.money-layout {
		display: flex;
		justify-content: space-between;
	}

	.money-item {
		display: flex;
		flex-direction: column
	}

	.money {
		text-align: center;
		color: #ffffff;
		font-size: 50rpx;
		margin-bottom: 20rpx;
	}

	.money-msg {
		text-align: center;
		color: #ffffff;
		font-size: 28rpx;
	}

	.sign-list {
		padding: 0 36rpx;
		display: flex;
		height: 100rpx;
		align-items: center
	}

	.signIcon {
		width: 30rpx;
		height: 30rpx;
	}

	.sign-msg {
		font-size: 26rpx;
		color: #999999;
		margin-left: 10rpx;
	}

	.list-item {
		height: 100rpx;
		font-size: 30rpx;
		color: #999999;
		padding: 0 40rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
		border-bottom: 1rpx solid #e5e5e5;
	}

	.sign-status {
		margin-left: 24rpx;
	}

	.org-font {
		color: #ff8e42;
	}

	.sign-statused {
		height: 94rpx;
		background: #ff8e42;
		color: #ffffff;
	}

	.mark-btn {
		font-size: 26rpx;
		color: #ffffff;
	}
	/deep/ .uni-table-tr:first-child{
		background: #ffbf65 !important;
		view{
			color: #FFFFFF !important;
		}
	}
	.ad-container{
		.title{
			display: flex;
			align-items: center;
			height: 50rpx;
			padding: 1rpx;
			font-weight: 900;
			view{
				width: 10rpx;
				height: 90%;
				background: #ff9f29;
			}
		}
		.ad{
			/deep/ ad{
				transform: scale(0.8) !important;
			}
		}
	}
</style>
